﻿



namespace Triad.Web
{
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using System;
    using System.Security.Principal;
    using System.Text;
    using Triad.BLL;
    using Triad.Common;

    /// <summary>
    /// THIS PAGE *CANNOT* INHERIT Triad.Web.UI.Page - that would be bad.
    ///(Creates an endless loop because a user hasn't been authenticated yet)
    /// </summary>
    public partial class Login : System.Web.UI.Page
    {

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks>
        /// Author: tim.fischer,   4/23/2008 - 9:11 AM
        /// Modified By: annette.hass,  5/20/2008
        /// </remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((ConfigurationManager.AppSettings["LocalDev"]) != "True")
            //{
            //    if (HttpContext.Current.User.Identity.AuthenticationType != "Basic")
            //    {
            //        Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            //        Response.AppendHeader("WWW-Authenticate", "Basic");
            //        Response.Write(
            //            "<script type='text/javascript'>document.execCommand('ClearAuthenticationCache',false,null);</script>");
            //        return;
            //    }
            //}
        }

        #region login authentication without captcha

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            // We need to determine if the user is authenticated and set e.Authenticated accordingly
            // Get the values entered by the user
            string loginUsername = Login1.UserName;
            string loginPassword = Login1.Password;

            // Next, determine if the user's username/password are valid
            if (Membership.ValidateUser(loginUsername, loginPassword))
            {
                FormsAuthentication.Authenticate(loginUsername, loginPassword);
                e.Authenticated = true;
                Session["UserObj"] = new framework().GetUserObject("1111",UserQueryType.EmployeeId);

            }
            else
            {
                e.Authenticated = false;
                Login1.FailureText = "Your username and/or password are invalid.";
            }
            
        }

        #endregion

        #region display failure message in clientside alert box

        protected void Login1_LoginError(object sender, EventArgs e)
        {
            //Display the failure message in a client-side alert box
            ClientScript.RegisterStartupScript(GetType(), "LoginError", string.Format("alert('ar{0}');", Login1.FailureText.Replace("'", "\\'")), true);
            Session["UserObj"] = null;
        }


        #endregion

        /// <summary>
        ///  needed for Windows 2000 compatibility
        /// Windows 2003 can bind using a S-1-xx-x-xx-xxx- format 
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>

        private static string SidToHex(SecurityIdentifier sid)
        {
            int binLength = sid.BinaryLength;
            var bt = new byte[binLength];
            sid.GetBinaryForm(bt, 0);
            var retval = new StringBuilder(binLength * 2, binLength * 2);
            for (int cx = 0; cx < binLength; cx++)
                retval.Append(bt[cx].ToString("X2"));
            return retval.ToString();
        }
    }
}


